﻿using Alimentador.Foods;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Alimentador.BottomGroup.Results
{
    /// <summary>
    /// Interaction logic for FatAcidsPieChart.xaml
    /// </summary>
    public partial class FatAcidsPieChart : UserControl
    {
        private List<FoodProperty> m_valueList = new List<FoodProperty>();
        private List<FoodBase> m_foodList = new List<FoodBase>();
        private int m_numDays = 1;
        private static bool m_IsShownInPopUp;

        /// <summary>
        /// Constructor
        /// </summary>
        public FatAcidsPieChart()
        {
            InitializeComponent();
            PopulateChart();
            RefreshChart();
        }

        private System.Collections.ObjectModel.Collection<System.Windows.Style> EnergyPalette(List<FoodProperty> list)
        {
            System.Collections.ObjectModel.Collection<System.Windows.Style> palette = new System.Collections.ObjectModel.Collection<System.Windows.Style>();
            foreach (FoodProperty item in list)
            {
                Style style = new Style(typeof(Control));
                SolidColorBrush brush = null;
                switch (item.PropertyName)
                {
                    case PropertyName.AcidosGordosSaturados: brush = new SolidColorBrush(Colors.Red); break;
                    case PropertyName.AcidosGordosMonoinsaturados: brush = new SolidColorBrush(Colors.Green); break;
                    case PropertyName.AcidosGordosPolinsaturados: brush = new SolidColorBrush(Color.FromRgb(10, 104, 245)); break;
                }
                style.Setters.Add(new Setter() { Property = Control.BackgroundProperty, Value = brush });
                palette.Add(style);
            }
            return palette;
        }

        /// <summary>
        /// Fill the chart with default values
        /// </summary>
        private void PopulateChart()
        {
            m_valueList.Add(new FoodProperty(PropertyName.AcidosGordosSaturados, 0));
            m_valueList.Add(new FoodProperty(PropertyName.AcidosGordosMonoinsaturados, 0));
            m_valueList.Add(new FoodProperty(PropertyName.AcidosGordosPolinsaturados, 0));

            pieChart.DataContext = m_valueList;
            pieChart.StylePalette = EnergyPalette(m_valueList);
            pieChart.FontSize = 10;
        }

        /// <summary>
        /// Refresh chart values
        /// </summary>
        public void RefreshChart()
        {
            foreach (FoodProperty fp in m_valueList)
            {
                if (fp.GetPropertyName == MainHelper.GetEnumDescription(PropertyName.AcidosGordosSaturados))
                {
                    fp.Quantity = FoodFormulas.FatAcidSaturatedPercentage(m_foodList);
                }
                else if (fp.GetPropertyName == MainHelper.GetEnumDescription(PropertyName.AcidosGordosMonoinsaturados))
                {
                    fp.Quantity = FoodFormulas.FatAcidMonoSaturatedPercentage(m_foodList);
                }
                else if (fp.GetPropertyName == MainHelper.GetEnumDescription(PropertyName.AcidosGordosPolinsaturados))
                {
                    fp.Quantity = FoodFormulas.FatAcidPoliSaturatedPercentage(m_foodList);
                }
            }

            pieChart.Refresh();
        }

        /// <summary>
        /// Hanlde the event for adding a Food to list
        /// </summary>
        /// <param name="foodList">List(Food)</param>
        /// <param name="numDays">int</param>
        internal void AddFood(List<FoodBase> foodList, int numDays = 1)
        {
            m_foodList = foodList;
            m_numDays = numDays;
            RefreshChart();
        }

        private void pieChart_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (!m_IsShownInPopUp)
            {
                TheMainGrid.Children.Remove(pieChart);

                ChartPopUp cpp = new ChartPopUp(pieChart);
                cpp.Owner = Application.Current.MainWindow;
                m_IsShownInPopUp = true;
                cpp.ShowDialog();

                pieChart = cpp.getChartBack();

                TheMainGrid.Children.Add(pieChart);
                m_IsShownInPopUp = false;   
            }
        }
    }
}
